#encoding=utf8
'''
Created on 2012-10-15

@author: Administrator
'''
from dispatch.Processor import Processor
from pub import Func
from pub import UserDict, RoomDict;
from pub.Config import Config

class RGAClass(Processor):
    '''
    指令：RGA(room game add)
    备注：玩家加入游戏的房间
    格式：c-s:cmd#cmdID#roomID#userID
     s-c:cmd #cmdID# result #roomID# roomState# userID# userNickName#msg
    '''
    def __init__(self,):
        
        Processor.__init__(self,);
        self.__roomId = 0;
        self.__userId = 0;

    
    def dataValidtor(self, cmdContent):
        if not cmdContent:
            return False;
        paraLen = len(cmdContent);
        if paraLen == 2:
            self.__roomId = Func.strToInt(cmdContent[0], Config.I_SYS_ZERO);
            self.__userId = Func.strToInt(cmdContent[1], Config.I_SYS_ZERO);
            return True;        
        return False;
    
    
            
    
    
    
    def outData(self,):
        if not Config.D_ROOM_DICT:
            Func.sendError(self._sockObj, self._cmdId, Config.ERROR_MESSAGE_ROOM_NULL + "");
            return False;
        room = RoomDict.getRoomFromDictById(self.__roomId);
        if not room:
            Func.sendError(self._sockObj, self._cmdId, Config.ERROR_MESSAGE_ROOM_DETAIL_NULL + ",roomId:" + Func.toStr(self.__roomId, 0));
            return False;
        room.set_state(Config.I_ROOM_STATE_READY);
            
        
        user = UserDict.getUserFromUserDictById(self.__userId);
        if not user:
            Func.sendError(self._sockObj, self._cmdId, Config.ERROR_MESSAGE_USER_DETAIL_NULL + ",userId:" + Func.toStr(self.__userId, 0));
            return False;
        
        result = Config.I_SYS_ZERO;
        msg = Config.ROOM_MESSAGE_CODE_ADD_FILL;
        userLen = len(room.get_user_list());
        if room.get_upper_limit() < userLen + 1:#达到房间上限
            msg = Config.ROOM_MESSAGE_CODE_UPPER_LIMIT; 
            sendStr = self.setPackage([result, self.__roomId, room.get_state(), user.get_user_id(), user.get_user_nick_name(), msg]);
            self._sockObj.sendLine(sendStr);
            return Config.I_SYS_ONE;
        returnResult = UserDict.userToSwitchRoom(self.__userId, self.__roomId);#切换或加入房间
        if returnResult:
            result = Config.I_SYS_ONE;
            msg = Config.ROOM_MESSAGE_CODE_ADD_SUSS;  
#        
        sendStr = self.setPackage([result, self.__roomId, room.get_state(), user.get_user_id(), user.get_user_nick_name(), msg]);
        room = RoomDict.getRoomFromDictById(self.__roomId);  
      
        Func.debugInfo("RGA send info:" + sendStr);  
        RoomDict.sendInfoToRoomAllUser(roomObj=room, cmd="", msg=sendStr, isPackage=False);     
       
        return Config.I_SYS_ONE;        
 
 
if __name__ == "__main__":
    pass;
    
        
